/*
 * @Description: 手动方式打开弹框
 * @Author: kanglin
 * @Date: 2018-08-30 14:22:41
 * @Copyright: Created by Panxsoft.
 */

import Vue from 'vue';
import MAlert from './m-alert';


class Modal {
	/**
     * 创建容器
     */
	static createModalContainer() {
		if (Modal.modalContainer) {
			return Modal.modalContainer;
		}
		Modal.modalContainer = document.createElement('div');
		Modal.modalContainer.id = 'm-modal';
		document.body.appendChild(Modal.modalContainer);
		return Modal.modalContainer;
	}

	static alert(options) {
		const alert = Vue.extend(MAlert);
		function noop() {}
		const AlertConstructor = alert.mixin({
			methods: {
				hide() {
					this.is_visible = false;
				},
			},
		});
		const instance = new AlertConstructor({
			propsData: {
				content: options.text,
				visible: true,
				ok: options.ok || noop,
				cancel: options.cancel || noop,
			},
		});
		instance.vm = instance.$mount();
		const modalContainer = Modal.createModalContainer();
		modalContainer.appendChild(instance.$el);
		return instance.vm;
	}
}

export default Modal;

